//
//  ZImportPrivateController.m
//  NewProject
//
//  Created by Apple on 2018/5/19.
//  Copyright © 2018年 chenIos. All rights reserved.
//
#import "ZMainController.h"
#import "ZImportPrivateController.h"
#import "ZWHtmlController.h"
@interface ZImportPrivateController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *privateBackView;

@end

@implementation ZImportPrivateController
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}
- (YYTextView *)privateTV
{
    if (!_privateTV) {
        YYTextView *tv = [YYTextView new];
        tv.placeholderText = @"明文私钥";
        [self.privateBackView addSubview:tv];
        [tv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(12);
            make.right.bottom.mas_equalTo(-12);
        }];
        _privateTV = tv;
    }return _privateTV;
}

- (IBAction)importAction:(UIButton *)sender {
    
  
    if ([self.passwordTF.text isEqualToString:self.reapeatPasswordTF.text]&&self.walletNameTF.text.length&&self.privateTV.text.length) {
         ZWWeakSelf;
        //不重名 导入钱包
        WewalletsdkWeWallet *result = [ZGRPC_Manager importPrivatekey:weakSelf.privateTV.text];
        if (result) {
            [ZDBManager createWallet:self.walletNameTF.text password:self.passwordTF.text address:result.address privateKey:weakSelf.privateTV.text complition:^(BOOL exist, BOOL addressExist, BOOL isFirst,BOOL createSuccess) {
                if (exist) {
                    [ZProgressManager showError:Z_Location(@"duplicateWalletName")];
                    return ;
                }
                if (addressExist) {
                    [ZProgressManager showSuccess:Z_Location(@"z_wallet_exist")];
                    return;
                }
                if (!createSuccess) {
                    [ZProgressManager showError:Z_Location(@"importWalletFail")];
                    return;
                }
                if (isFirst) {
                    [UIApplication sharedApplication].keyWindow.rootViewController = [ZMainController new];
                }else
                {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"z_wallet_pais_change" object:nil];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }
                
            }];
        }else
        {
             [ZProgressManager showError:@"私钥有误"];
            
        }
        
        ZWLog(@"private == %@",self.privateTV.text);
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (IBAction)agreeProtocolAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.isSelected) {
        self.importBtn.backgroundColor = [UIColor colorWithHexString:@"#37b9c9"];
        
    }else
    {
        self.importBtn.backgroundColor = RGBCOLOR(214, 214, 214);
    }
    
}
- (IBAction)showProtocolAction:(UIButton *)sender {
    ZWHtmlController *htmlControl = [ZWHtmlController new];
    
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:Z_Location(@"protocolArticle")
                                                         ofType:@"html"];
    NSString * htmlStr = [NSString stringWithContentsOfFile:htmlPath
                                                   encoding:NSUTF8StringEncoding
                                                      error:nil];
    
    htmlControl.urlStr = htmlStr;
    htmlControl.titleStr = Z_Location(@"protocal");
    [self.navigationController pushViewController:htmlControl animated:YES];
}
- (void)initUI{
    for (UIView *segment in self.segmentView) {
        segment.backgroundColor = COLOR_LIGHT_GRAY;
    }
    
    [self privateTV];
    self.privateBackView.layer.borderWidth = 1;
    self.privateBackView.layer.borderColor = [UIColor colorWithHexString:@"#f2f2f2"].CGColor;
    self.title = Z_Location(@"importWallet");
    self.privateTV.placeholderText = Z_Location(@"inputPrivateKey");
    self.passwordTF.placeholder = Z_Location(@"password");
    self.walletNameTF.placeholder = Z_Location(@"walletName");
    self.reapeatPasswordTF.placeholder = Z_Location(@"repeatPassword");
    [self.agreeBtn setTitle:Z_Location(@"agreeOn") forState:UIControlStateNormal];
    [self.protocolBtn setTitle:Z_Location(@"termsOfServiceAndPrivacyPolicy") forState:UIControlStateNormal];
    [self.agreeBtn setTitleColor:[UIColor colorWithHexString:@"#4e5e6e"] forState:UIControlStateNormal];
    [self.protocolBtn setTitleColor:[UIColor colorWithHexString:@"#37b9c9"] forState:UIControlStateNormal];
    [self.importBtn setBackgroundColor:RGBCOLOR(214, 214, 214)];
    self.privateTV.backgroundColor = [UIColor whiteColor];
    [self.importBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.importBtn.titleLabel.font = H_Medium2;
    self.importBtn.layer.cornerRadius = 5;
    self.importBtn.clipsToBounds = YES;
    [self.importBtn setTitle:Z_Location(@"startImporting") forState:UIControlStateNormal];
//    self.importBtn setBackgroundColor:<#(UIColor * _Nullable)#>
    self.agreeBtn.titleLabel.font = H_Medium6;
    self.protocolBtn.titleLabel.font = H_Medium6;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
